package com.lwz.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwz.educenter.entity.UcenterMember;
import com.lwz.educenter.entity.Vo.RegisterVo;
import com.lwz.educenter.mapper.UcenterMemberMapper;
import com.lwz.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwz.servicebase.exceptionhandler.MyException;
import com.lwz.util.JwtInfo;
import com.lwz.util.JwtUtil;
import com.lwz.util.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Lw中
 * @since 2020-11-18
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    /**
     * LWZ TODO : 2020/11/18 登录注册业务逻辑层
     */

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 验证用户登录信息后使用用户的密码和手机号生成token值
     * @param ucenterMember
     * @return
     */
    @Override
    public String login(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new MyException(20001, "登录失败!");
        }
        // 验证手机号码是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember member = baseMapper.selectOne(wrapper);
        // 判断查询到的用户是否为空
        if (member == null) {
            throw new MyException(20001, "手机号码错误，登录失败!");
        }
        // 判断密码（将用户输入的密码加密再和数据库中的密码比较）
        if (!MD5.encrypt(password).equals(member.getPassword())) {
            throw new MyException(20001, "密码错误，登录失败");
        }
        // 判断用户是否被禁用
        if (member.getIsDeleted()) {
            throw  new MyException(20001, "当前用户不可用，登录失败");
        }
        // 通过用户的id和密码生成token值
//        String jwtToken = JwtUtil.getJwtToken(member.getId(), member.getNickname());
        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(member.getId());
        jwtInfo.setNickname(member.getNickname());
        jwtInfo.setAvatar(member.getAvatar());
        String jwtToken = JwtUtil.getJwtToken(jwtInfo);
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 获取注册的数据
        // 用户的code码
        String code = registerVo.getCode();
        // 用户的手机号码
        String mobile = registerVo.getMobile();
        // 用户昵称
        String nickname = registerVo.getNickname();
        // 用户密码
        String password = registerVo.getPassword();
        // 判断用户输入的数据是否为空，只要有一个数据为空注册就是失败
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new MyException(20001, "请填充所有数据，注册失败！");
        }
        // 判断验证码是否过期
        String redisCode = redisTemplate.opsForValue().get("phone");
        if (!code.equals(redisCode)) {
            throw new MyException(20001, "验证码过期或者错误，注册失败！");
        }
        // 判断手机号码是个重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count >= 1) {
            throw new MyException(20001, "手机号重复，注册失败！");
        }
        // 将用户数据添加至数据库中
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(nickname);
        ucenterMember.setMobile(mobile);
        // 设置用户头像
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        ucenterMember.setIsDeleted(false);
        // 插入数据库前需要给用户密码进行加密
        ucenterMember.setPassword(MD5.encrypt(password));
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public int countRegisterDay(String day) {
        int count = baseMapper.countRegisterDay(day);
        return count;
    }
}
